package com.flexfiltering.view.controls.completionInputClasses
{
	
import flash.events.Event;

import mx.collections.ArrayCollection;
import mx.controls.List;
import mx.core.ClassFactory;
import mx.core.EdgeMetrics;
import mx.events.CollectionEvent;
import mx.events.CollectionEventKind;

public class CompletionInputMenu extends List
{
	public function CompletionInputMenu()
	{
		super();
		itemRenderer = new ClassFactory(CompletionInputMenuItemRender);
	}
	
	//----------------------------------
	//  viewMetrics
    //----------------------------------
	
	override public function get viewMetrics():EdgeMetrics
	{
		var vm:EdgeMetrics = super.viewMetrics.clone();
		
		var cr:Number = getStyle("cornerRadius");
		
		if ( cr && cr > 0 )
		{
			vm.top += cr;
			vm.bottom += cr;
		}
		
		return vm;
	}
	
	//----------------------------------
	//  measure
	//----------------------------------
	
	override protected function measure():void
	{
		super.measure();
		
		var len:Number = collection ? collection.length : 8;
		
		measuredHeight = measuredMinHeight = 
			Math.min(len,8) * rowHeight + viewMetrics.top + viewMetrics.bottom;
	}
	
	override protected function commitProperties():void
	{
		super.commitProperties()
		
		if ( collectionReset )
		{
			selectedIndex = 0;
		}	
	}
	
	protected var collectionReset:Boolean;
	
	override protected function collectionChangeHandler(event:Event):void
	{
        super.collectionChangeHandler(event);
        
        var ce:CollectionEvent = CollectionEvent(event);
     	
     	if ( ce.kind == CollectionEventKind.RESET ||
     		 ce.kind == CollectionEventKind.REFRESH )
     	{
     		collectionReset = true;
     		invalidateSize();
     		selectedIndex = 0;
     		
     	}   
	}
	
} // class
} // package